import TextUtil from "./text";
import Channel from "./channel";

let _id = uni.getStorageSync("user_id") ?? -1
let _rid = uni.getStorageSync("user_rid") ?? ""
let _code = uni.getStorageSync("user_code") ?? ""

class UserUtil {

  static getId() {
    if (process.env.NODE_ENV === 'development') {
      return 1
    }
    return Channel.getId(_id);
  }

  static _setId(id) {
    _id = id
    uni.setStorageSync("user_id", id)
  }

  static getRid() {
    return Channel.getRid(_rid);
  }

  static _setRid(rid) {
    _rid = rid
    uni.setStorageSync("user_rid", rid)
  }

  static setCode(code) {
    _code = code
    uni.setStorageSync("user_code", code)
  }

  static getCode() {
    return Channel.getCode(_code)
  }

  static setRecommendCode(code) {
    if (!TextUtil.isEmpty(code)) {
      UserUtil._setRecommendCode(code)
    }
  }

  static _setRecommendCode(code) {
    uni.setStorageSync("user_recommend_code", code)
  }

  static getRecommendCode() {
    return uni.getStorageSync("user_recommend_code") ?? ""
  }

  static login(id, rid) {
    UserUtil._setId(id)
    UserUtil._setRid(rid)
  }

  static exit() {
    UserUtil._setId(-1)
    UserUtil._setRid("")
    UserUtil._setRecommendCode("")
    UserUtil.setCode("")
  }

  static defaultUser() {
    return {
      recommend_channel: "",
      coinsDay: 0,
      messageCount: 0,
      code: "",
      level: 1,
      recommend_id1: UserUtil.isLogin() ? 1 : 0,
      photo: "/static/common/image/icon_user_default.png",
      relation_id: 0,
      isSign: false,
      coinsBalance: 0,
      friends_num: 0,
      coinsMonth: 0,
      name: UserUtil.isLogin() ? "登录中..." : "立即登录"
    }
  }

  static isLogin() {
    return this.getId() > 0
  }
}

export default UserUtil